package com.cduestc.mylogin;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;

public class TheTimer extends AppCompatActivity {
    Chronometer cTimer;
    Button begin,stop,regain,resetT,timerBack;
    private long recodeTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_the_timer);

        cTimer = this.findViewById(R.id.cTimer);
        timerBack = this.findViewById(R.id.timerBack);
        begin = this.findViewById(R.id.begin);
        stop = this.findViewById(R.id.stop);
        regain = this.findViewById(R.id.regain);
        resetT = this.findViewById(R.id.resetT);

        stop.setEnabled(false);
        stop.setBackgroundColor(Color.parseColor("#66ccff"));
        regain.setEnabled(false);
        regain.setBackgroundColor(Color.parseColor("#66ccff"));
        resetT.setEnabled(false);
        resetT.setBackgroundColor(Color.parseColor("#66ccff"));

        timerBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(TheTimer.this,HomePage.class);
                startActivity(intent);
            }
        });

        begin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cTimer.setBase(SystemClock.elapsedRealtime());
                cTimer.start();
                begin.setEnabled(false);
                begin.setBackgroundColor(Color.parseColor("#66ccff"));
                stop.setEnabled(true);
                stop.setBackgroundColor(Color.parseColor("#0000ff"));
                regain.setEnabled(false);
                regain.setBackgroundColor(Color.parseColor("#66ccff"));
                resetT.setEnabled(true);
                resetT.setBackgroundColor(Color.parseColor("#0000ff"));
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cTimer.stop();
                recodeTime = SystemClock.elapsedRealtime();
                begin.setEnabled(false);
                begin.setBackgroundColor(Color.parseColor("#66ccff"));
                stop.setEnabled(false);
                stop.setBackgroundColor(Color.parseColor("#66ccff"));
                regain.setEnabled(true);
                regain.setBackgroundColor(Color.parseColor("#0000ff"));
                resetT.setEnabled(true);
                resetT.setBackgroundColor(Color.parseColor("#0000ff"));
            }
        });

        regain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cTimer.setBase(cTimer.getBase() + (SystemClock.elapsedRealtime() - recodeTime));
                cTimer.start();
                begin.setEnabled(false);
                begin.setBackgroundColor(Color.parseColor("#66ccff"));
                stop.setEnabled(true);
                stop.setBackgroundColor(Color.parseColor("#0000ff"));
                regain.setEnabled(false);
                regain.setBackgroundColor(Color.parseColor("#66ccff"));
                resetT.setEnabled(true);
                resetT.setBackgroundColor(Color.parseColor("#0000ff"));
            }
        });

        resetT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cTimer.start();
                cTimer.setBase(SystemClock.elapsedRealtime());
                recodeTime = 0;
                begin.setEnabled(false);
                begin.setBackgroundColor(Color.parseColor("#66ccff"));
                stop.setEnabled(true);
                stop.setBackgroundColor(Color.parseColor("#0000ff"));
                regain.setEnabled(false);
                regain.setBackgroundColor(Color.parseColor("#66ccff"));
                resetT.setEnabled(false);
                resetT.setBackgroundColor(Color.parseColor("#66ccff"));
            }
        });

        cTimer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            public void onChronometerTick(Chronometer ch) {
                //超过一小时自动停止
                if (SystemClock.elapsedRealtime() - ch.getBase() > 1*60*60*1000) {
                    ch.stop();
                    begin.setClickable(true);
                    begin.setBackgroundColor(Color.parseColor("#0000ff"));
                    stop.setClickable(false);
                    stop.setBackgroundColor(Color.parseColor("#66ccff"));
                    regain.setClickable(false);
                    regain.setBackgroundColor(Color.parseColor("#66ccff"));
                }
            }
        });
    }
}